package com.example.practice.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.practice.R;
import com.example.practice.activity.ExerciseDetailActivity;
import com.example.practice.bean.ExercisesBean;

import java.util.List;

public class ExcerciseAdapter_yl extends BaseAdapter {
    private List<ExercisesBean> listYl;
    private Context contextYl;

    public ExcerciseAdapter_yl(Context contextYl) {
        this.contextYl = contextYl;
    }

    //create data resource function
    public void setData(List<ExercisesBean> list) {
        listYl=list;
        notifyDataSetChanged();
    }

    //according to item  to create class ViewHolder
    class ViewHolder {
        TextView title;
        TextView num;
        TextView order;
    }

    @Override
    public int getCount() {
        return listYl==null?0:listYl.size();
    }

    @Override
    public Object getItem(int position) {
        return listYl==null?null:listYl.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        if (convertView==null){
            vh=new ViewHolder();
            convertView= LayoutInflater.from(contextYl).inflate(R.layout.excecise_list_item_yl,null);
            vh.title=convertView.findViewById(R.id.exercises_item_title_yl);
            vh.num=convertView.findViewById(R.id.exercises_item_content_yl);
            vh.order=convertView.findViewById(R.id.tv_order_yl);
            convertView.setTag(vh);
        }
        else {
            vh= (ViewHolder) convertView.getTag();
        }
//        获取position对应的Item数据对象
        final ExercisesBean bean= (ExercisesBean) getItem(position);
        if (bean!=null){
            vh.order.setText(position+1+"");
            vh.title.setText(bean.title);
            vh.num.setText(bean.content);
            vh.order.setBackgroundResource(bean.background);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean==null){
                    return;
                }
                Intent intent= new Intent(contextYl, ExerciseDetailActivity.class);
                intent.putExtra("id",bean.id);
                intent.putExtra("title",bean.title);
                contextYl.startActivity(intent);
            }
        });
        return convertView;
    }
}
